package ServerStuff;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.sql.Date;
import java.util.Calendar;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;

import DataStorageClasses.Job;
import Database.DatabaseConnector;

@WebServlet("/AddJobServlet")
public class AddJobServlet extends HttpServlet
{
	private static final long serialVersionUID = 1L;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
	{
		DatabaseConnector db = new DatabaseConnector();
		String business = (String) request.getSession().getAttribute("username");
		double estPrice=0;
		double actPrice=0;
		double amtPaid=0;
		long clientID=-1;
		String expStart = request.getParameter("expSDate");
		String actStart = request.getParameter("actSDate");
		String expFinish = request.getParameter("expFDate");
		String actFinish = request.getParameter("actFDate");
		String street = request.getParameter("street");
		String province = request.getParameter("prov");
		String city = request.getParameter("city");
		Date expStartDate=null, actStartDate=null, expFinishDate=null, actFinishDate=null;
		RequestDispatcher dispatcher;
		boolean failed = false;
		try {
			clientID = Long.parseLong(request.getParameter("clientid"));
			estPrice = Double.parseDouble(request.getParameter("estP"));
			actPrice = Double.parseDouble(request.getParameter("actP"));
			amtPaid = Double.parseDouble(request.getParameter("amtP"));
		} catch(NullPointerException e) {
			request.setAttribute("failed", true);
			request.setAttribute("message", "Must Put a Number in Monetary Fields");
			failed = true;
		} catch (NumberFormatException e) {
			request.setAttribute("failed", true);
			request.setAttribute("message", "Must Put a Number in Monetary Fields");
			failed = true;
		}
		try {
			Calendar calendar = Calendar.getInstance();
			if(expStart != null && !expStart.isEmpty()) {
				String[] date = expStart.split("-");
				calendar.set(Integer.parseInt(date[0]), Integer.parseInt(date[1])-1, Integer.parseInt(date[2]));
				expStartDate = new Date(calendar.getTimeInMillis());
			}
			
			if(actStart != null && !actStart.isEmpty()) {
				String[] date = actStart.split("-");
				calendar.set(Integer.parseInt(date[0]), Integer.parseInt(date[1]), Integer.parseInt(date[2]));
				actStartDate = new Date(calendar.getTimeInMillis());
			}
			
			if(expFinish != null && !expFinish.isEmpty()) {
				String[] date = expFinish.split("-");
				calendar.set(Integer.parseInt(date[0]), Integer.parseInt(date[1]), Integer.parseInt(date[2]));
				expFinishDate = new Date(calendar.getTimeInMillis());
			}
			
			if(actFinish != null && !actFinish.isEmpty()) {
				String[] date = actFinish.split("-");
				calendar.set(Integer.parseInt(date[0]), Integer.parseInt(date[1]), Integer.parseInt(date[2]));
				actFinishDate = new Date(calendar.getTimeInMillis());
			}
		}
		catch(NullPointerException e) {
			request.setAttribute("failed", true);
			request.setAttribute("message", "Invalid Dates");
			failed = true;
		} catch (NumberFormatException e) {
			request.setAttribute("failed", true);
			request.setAttribute("message", "Must Put a Number in Monetary Fields");
			failed = true;
		}
		
		if (!failed) {
			try {
				Job job = db.addNewJob(clientID, estPrice, actPrice, amtPaid, expStartDate, actStartDate, expFinishDate, actFinishDate, street, city, province, business);
				if (job!=null) {
					response.sendRedirect("/GoGoGadget/JobInfo.jsp?jobID=" + job.getJobID());
					return;
				}
				request.setAttribute("failed", true);
				request.setAttribute("message", "Job Could Not Be Created. Client ID Might Be Invalid");
			} catch (IOException e) {
				request.setAttribute("failed", true);
				request.setAttribute("message", "An Unexpected Error Occured. Job Not Created. Try Again Later");
				dispatcher = request.getRequestDispatcher("/CreateJob.jsp");
			}
		}
		
		dispatcher = request.getRequestDispatcher("/CreateJob.jsp");
		try {
			dispatcher.forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		response.setStatus(HttpURLConnection.HTTP_ACCEPTED);
	}
}
